package com.wskj.common.utils.tencent;

import com.alibaba.fastjson.JSON;
import com.wskj.common.config.TencentConfig;
import com.tencent.xinge.XingeApp;
import com.tencent.xinge.bean.*;
import com.tencent.xinge.bean.ios.Alert;
import com.tencent.xinge.bean.ios.Aps;
import com.tencent.xinge.push.app.PushAppRequest;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * 腾讯移动推送 工具类
 *
 * @author Kun
 * @date 2021/11/09
 */
@Slf4j
public class PushUtils {

    /**
     * 中国香港服务接入点
     */
    private static final String serviceUrl = "https://api.tpns.hk.tencent.com/";

    /**
     * 用户端-iOS-android 标签推送示例
     *
     * @return
     */
    public static void pushTagIosAndAndroidUser(TencentConfig tencentConfig, String title, String content, String customContent, Long visitUserId, String[]... intent) {
        XingeApp xingeApp_ios = new XingeApp.Builder()
                .appId(tencentConfig.getIosUserAccessID())
                .secretKey(tencentConfig.getIosUserSecretKey())
                .domainUrl(serviceUrl)
                .build();
        PushAppRequest pushAppRequest_ios = new PushAppRequest();
        pushAppRequest_ios.setAudience_type(AudienceType.tag);
        pushAppRequest_ios.setEnvironment(Environment.valueOf(tencentConfig.getEnvironment()));
        pushAppRequest_ios.setMessage_type(MessageType.notify);
        Message message_ios = new Message();
        message_ios.setTitle(title);
        message_ios.setContent(content);
        MessageIOS messageIOS = new MessageIOS();
        Alert alert = new Alert();
        Aps aps = new Aps();
        aps.setAlert(alert);
        messageIOS.setAps(aps);
        // start
        messageIOS.setCustom(customContent);
        // end
        message_ios.setIos(messageIOS);
        pushAppRequest_ios.setMessage(message_ios);
        ArrayList<String> tagList_ios = new ArrayList();
        tagList_ios.add(visitUserId+"");
        TagListObject tagListObject_ios = new TagListObject();
        tagListObject_ios.setTags(tagList_ios);
        tagListObject_ios.setOp(OpType.OR);
        pushAppRequest_ios.setMessage(message_ios);
        pushAppRequest_ios.setTag_list(tagListObject_ios);
        JSONObject jsonObject_ios = xingeApp_ios.pushApp(pushAppRequest_ios);
        log.info("用户端-移动推送-ios-结果:{}", JSON.toJSONString(jsonObject_ios));

        // 安卓
        XingeApp xingeApp_android = new XingeApp.Builder()
                .appId(tencentConfig.getAndroidUserAccessID())
                .secretKey(tencentConfig.getAndroidUserSecretKey())
                .domainUrl(serviceUrl)
                .build();
        PushAppRequest pushAppRequest_android= new PushAppRequest();
        pushAppRequest_android.setAudience_type(AudienceType.tag);
        pushAppRequest_android.setPlatform(Platform.android);
        pushAppRequest_android.setMessage_type(MessageType.notify);
        Message message_android = new Message();
        message_android.setTitle(title);
        message_android.setContent(content);
        MessageAndroid messageAndroid = new MessageAndroid();
        if(intent != null && intent.length>0){
            ClickAction clickAction = new ClickAction();
            clickAction.setAction_type(3);
            clickAction.setIntent(String.valueOf(intent[0]));
            messageAndroid.setAction(clickAction); 
        }
        message_android.setAndroid(messageAndroid);
        pushAppRequest_android.setMessage(message_android);
        ArrayList<String> tagList_android = new ArrayList();
        tagList_android.add(visitUserId+"");
        TagListObject tagListObject_android = new TagListObject();
        tagListObject_android.setTags(tagList_android);
        tagListObject_android.setOp(OpType.OR);
        pushAppRequest_android.setTag_list(tagListObject_android);
        JSONObject jsonObject_android = xingeApp_android.pushApp(pushAppRequest_android);
        log.info("用户端-移动推送-android-结果:{}", JSON.toJSONString(jsonObject_android));
    }

    /**
     * Android-IOS-用户端 全部设备推送示例
     * @return
     */
    public static void pushAllIosAndAndroidUser(TencentConfig tencentConfig, String title,String content) {
        XingeApp xingeApp_ios = new XingeApp.Builder()
                .appId(tencentConfig.getIosUserAccessID())
                .secretKey(tencentConfig.getIosUserSecretKey())
                .domainUrl(serviceUrl)
                .build();
        PushAppRequest pushAppRequest_ios  = new PushAppRequest();
        pushAppRequest_ios.setAudience_type(AudienceType.all);
        pushAppRequest_ios.setEnvironment(Environment.valueOf(tencentConfig.getEnvironment()));
        pushAppRequest_ios.setMessage_type(MessageType.notify);
        Message message_ios  = new Message();
        message_ios.setTitle(title);
        message_ios.setContent(content);
        MessageIOS messageIOS = new MessageIOS();
        Alert alert = new Alert();
        Aps aps = new Aps();
        aps.setAlert(alert);
        messageIOS.setAps(aps);
        message_ios.setIos(messageIOS);
        pushAppRequest_ios.setMessage(message_ios);
        JSONObject jsonObject_ios = xingeApp_ios.pushApp(pushAppRequest_ios);
        log.info("用户端-全部移动推送-ios-结果:{}", JSON.toJSONString(jsonObject_ios));

        // 安卓
        XingeApp xingeApp_android = new XingeApp.Builder()
                .appId(tencentConfig.getAndroidUserAccessID())
                .secretKey(tencentConfig.getAndroidUserSecretKey())
                .domainUrl(serviceUrl)
                .build();
        PushAppRequest pushAppRequest_android = new PushAppRequest();
        pushAppRequest_android.setAudience_type(AudienceType.all);
        pushAppRequest_android.setPlatform(Platform.android);
        pushAppRequest_android.setMessage_type(MessageType.notify);
        Message message_android = new Message();
        message_android.setTitle(title);
        message_android.setContent(content);
        MessageAndroid messageAndroid = new MessageAndroid();
        message_android.setAndroid(messageAndroid);
        pushAppRequest_android.setMessage(message_android);
        JSONObject jsonObject_android = xingeApp_android.pushApp(pushAppRequest_android);
        log.info("用户端-全部移动推送-android-结果:{}", JSON.toJSONString(jsonObject_android));
    }

    public static void main(String[] args) {
        // System.out.println(JSON.toJSONString(PushUtils.pushTagIosUser()));
    }
}
